package com.samuel.blog.controller.admin;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.samuel.blog.bean.PageBean;
import com.samuel.blog.constant.CommonConstant;
import com.samuel.blog.controller.BaseController;
import com.samuel.blog.entity.JurisdictionEntity;
import com.samuel.blog.service.JurisdictionService;
import com.samuel.blog.utils.StringUtils;

@RequestMapping("admin")
@Controller
public class JurisdictionController extends BaseController {

	private static final long serialVersionUID = -6809854179508671834L;
	private static final Logger logger = Logger.getLogger(JurisdictionController.class);
	private static final String PRE = "jur/";
	private static final String PRE_PAGER = "admin/jur_";
	
	@Resource(name = "jurisdictionService")
	private JurisdictionService jurisdictionService;

	
	/**
	 * 进入权限列表页
	 * @param request
	 * @return
	 */
	@RequestMapping(value = PRE+"list"+SUFFIX, method = RequestMethod.GET)
	public String toList(HttpServletRequest request) {
		this.configCss(request, "jurisdiction", "jurisdiction_list");
		return PRE_PAGER+"list";
	}

	@ResponseBody
	@RequestMapping(value = PRE+"list"+SUFFIX, method = RequestMethod.POST)
	public String list(PageBean pageBean, HttpServletResponse response) {
		PageInfo<JurisdictionEntity> pageInfo = jurisdictionService.selectPage(pageBean, null);
		for (JurisdictionEntity jur : pageInfo.getList()) {
			if (!"0".equals(jur.getParentId())) {
				jur.setParentEntity(jurisdictionService.selectById(jur.getParentId()));
			}
		}
		this.returnTableData(pageBean, pageInfo, response);
		return null;
	}
	
	/**
	 * 进入添加页面
	 * @param request
	 * @return
	 */
	@RequestMapping(value = PRE+"add"+SUFFIX, method = RequestMethod.GET)
	public String add(HttpServletRequest request) {
		this.configCss(request, "jurisdiction", "jurisdiction_list");
		// 查询全部的顶级权限
		List<JurisdictionEntity> jurTopList = jurisdictionService.selectByTop();
		request.setAttribute("jurTopList", jurTopList);
		return PRE_PAGER + "input";
	}
	
	/**
	 * 进入编辑页面
	 * @param request
	 * @return
	 */
	@RequestMapping(value = PRE+"edit"+SUFFIX, method = RequestMethod.GET)
	public String edit(HttpServletRequest request, String id) {
		this.configCss(request, "jurisdiction", "jurisdiction_list");
		if (StringUtils.empty(id)) {
			logger.error("id 是必须的参数！");
			request.setAttribute("message", "请刷新页面重试！");
			return MESSAGE_PAGE;
		}
		JurisdictionEntity jur = jurisdictionService.selectById(id);
		if (null == jur || StringUtils.empty(jur.getJurisdictionId())) {
			logger.error("对象不存在！");
			request.setAttribute("message", "该数据不存在， 请刷新页面重试！");
			return MESSAGE_PAGE;
		}
		// 查询全部的顶级权限
		List<JurisdictionEntity> jurTopList = jurisdictionService.selectByTop();
		request.setAttribute("jurTopList", jurTopList);
		request.setAttribute("jur", jur);
		
		return PRE_PAGER + "input";
	}
	
	/**
	 * 保存对象
	 * @param jurisdictionEntity
	 * @param response
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = PRE+"save"+SUFFIX, method = RequestMethod.POST)
	public String save(JurisdictionEntity jurisdictionEntity, HttpServletResponse response) {
		String message = "";
		if (null == jurisdictionEntity) {
			message = "请填写必填项！";
		} else if (!StringUtils.empty(jurisdictionEntity.getJurisdictionId())) {
			message = "请刷新页面重试！";
		} else if (StringUtils.empty(jurisdictionEntity.getCode())) {
			message = "请输入编码！";
		} else if (StringUtils.empty(jurisdictionEntity.getParentId())) {
			message = "请选择上级权限！";
		} else if (StringUtils.empty(jurisdictionEntity.getName())) {
			message = "请输入名称！";
		} else {
			// 检测代码是否输入过
			JurisdictionEntity jur = jurisdictionService.selectByCode(jurisdictionEntity.getCode());
			if (null != jur) {
				message = jurisdictionEntity.getCode() + "编码已存在， 请重新输入！";
			}
		}
		if (!StringUtils.empty(message)) {
			logger.error(message);
			ajax(response, CommonConstant.NoticeCode.ERROR_ZERO, message);
			return null;
		}
		logger.info("--------------权限实体验证成功！----------");
		jurisdictionEntity.setModifyDate(new Date());
		jurisdictionEntity.setCreateDate(new Date());
		int num = jurisdictionService.insert(jurisdictionEntity);
		
		if (num > 0) {
			ajax(response, CommonConstant.NoticeCode.SUCCESS_ONE, "保存权限成功！");
		} else {
			ajax(response, CommonConstant.NoticeCode.ERROR_ZERO, "系统错误，保存权限失败！");
		}
		return null;
	}
	
	/**
	 * 根据条件修改
	 * @param jurisdictionEntity
	 * @param response
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = PRE+"update"+SUFFIX, method = RequestMethod.POST)
	public String update(JurisdictionEntity jurisdictionEntity, HttpServletResponse response) {
		String message = "";
		if (null == jurisdictionEntity) {
			message = "请填写必填项！";
		} else if (StringUtils.empty(jurisdictionEntity.getJurisdictionId())) {
			logger.error("id 是必须的！");
			message = "请刷新页面重试！";
		} else if (StringUtils.empty(jurisdictionEntity.getCode())) {
			message = "请输入编码！";
		} else if (StringUtils.empty(jurisdictionEntity.getParentId())) {
			message = "请选择上级权限！";
		} else if (StringUtils.empty(jurisdictionEntity.getName())) {
			message = "请输入名称！";
		} else {
			// 检测代码是否输入过
			JurisdictionEntity jur = jurisdictionService.selectByCode(jurisdictionEntity.getCode());
			if (null != jur && !jur.getJurisdictionId().equals(jurisdictionEntity.getJurisdictionId())) {
				message = jurisdictionEntity.getCode() + "编码已存在， 请重新输入！";
			}
		}
		if (!StringUtils.empty(message)) {
			logger.error(message);
			ajax(response, CommonConstant.NoticeCode.ERROR_ZERO, message);
			return null;
		}
		logger.info("--------------权限实体验证成功！----------");
		jurisdictionEntity.setModifyDate(new Date());
		int num = jurisdictionService.updateById(jurisdictionEntity);
		
		if (num > 0) {
			ajax(response, CommonConstant.NoticeCode.SUCCESS_ONE, "修改权限成功！");
		} else {
			ajax(response, CommonConstant.NoticeCode.ERROR_ZERO, "系统错误，修改权限失败！");
		}
		return null;
	}
	
	/**
	 * 批量删除
	 * @param response
	 * @param ids
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = PRE+"delete"+SUFFIX, method = RequestMethod.POST)
	public String delete(HttpServletResponse response, @RequestParam("ids[]") String[] ids) {
		String message = "";
		if (null == ids || ids.length <= 0) {
			message = "请至少选择一项！";
			logger.error(message);
			ajax(response, CommonConstant.NoticeCode.ERROR_ZERO, message);
			return null;
		} 
		
		int num = jurisdictionService.delete(ids);

		if (num > 0) {
			ajax(response, CommonConstant.NoticeCode.SUCCESS_ONE, "修改权限成功！");
		} else {
			ajax(response, CommonConstant.NoticeCode.ERROR_ZERO, "系统错误，修改权限失败！");
		}

		return null;
	}
	
}
